package com.lcy.mybatis.generator.plugin.element;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;

/**
 * 根据某一列进行选择：
 * select * from table where column = value
 *
 * <p>创建时间：2017/5/4 16:57</p>
 *
 * @author 娄存银
 * @version 1.0
 */
public class SelectByColumnElement extends CustomerSqlElement{
    private static final String SQL = "select\n" +
            "    <include refid=\"Base_Column_List\" /> \n" +
            "    from %s \n" +
            "        <where>\n" +
            "            <if test=\"column == null\">\n" +
            "                false\n" +
            "            </if>\n" +
            "            <if test=\"column != null and value != null \">\n" +
            "                ${column} = #{value}\n" +
            "            </if>\n" +
            "            <if test=\"column != null and value == null\">\n" +
            "                ${column} is null\n" +
            "            </if>\n" +
            "        </where>"+
            "    <if test=\"limit != null and limit.length() > 0\">\n" +
            "    limit ${limit}\n" +
            "    </if>";

    public SelectByColumnElement(IntrospectedTable table) {
        super(table);
    }

    @Override
    public void initXmlElement() {
        xmlElement = selectElement();
    }

    @Override
    protected void initMethod() {
        method = new Method(getId());

        method.setReturnType(modelListType());

        method.addParameter(new Parameter(TYPE_STRING,COLUMN,annotation(COLUMN)));
        method.addParameter(new Parameter(TYPE_OBJECT,VALUE,annotation(VALUE)));
        method.addParameter(new Parameter(TYPE_STRING,LIMIT,annotation(LIMIT)));

        imports.add(TYPE_LIST);
    }

    @Override
    protected String getId() {
        return "selectByColumn";
    }

    @Override
    protected String getSql() {
        return SQL;
    }
}
